package com.youxue.controller;

import com.alibaba.fastjson.JSONObject;
import com.cangshi.permission.entity.UserSubject;
import com.youxue.entity.*;
import com.youxue.permission.saver.SubjectHolder;
import com.youxue.service.ClassService;
import com.youxue.service.LineService;
import com.youxue.service.OrderService;
import com.youxue.service.UserClassService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.ArrayList;
import java.util.List;

/**
 * Created by 15c on 2018/1/20.
 */
@Controller
@RequestMapping("/line")
public class LineController {
    @Autowired
    LineService lineService;
    @Autowired
    ClassService classService;
    @Autowired
    OrderService orderService;
    @Autowired
    UserClassService userClassService;

    /**
     * 展示一门课程下的所有路线信息
     * 此处需要将请求头token加上
     *
     * @param classId
     * @return
     */
    @ResponseBody
    @RequestMapping(value = "/aclass", method = RequestMethod.GET, produces = "text/html;charset=UTF-8")
    String getAClassAllLine(@RequestParam(required = false) Integer classId) {
        if (null == classId) {
            ErrorJsonObject errorJsonObject = new ErrorJsonObject(101, "classId参数为空");
            return JSONObject.toJSONString(errorJsonObject);
        }
        //获得课程价格
        double price = classService.showAClassInfo(classId).getClassPrice();
        List<Line> lineList = new ArrayList<Line>();
        if (price != 0) {
            int userId;
            try {
                // 获取当前访问对象
                UserSubject subject = (UserSubject) SubjectHolder.getSubject();

                //获取当前的登录信息
                UserEntity userEntity = (UserEntity) subject.getAuthorizationEntity();
                //获取当前的用户登录id
                userId = userEntity.getUserId();
            } catch (NullPointerException e) {
                ErrorJsonObject errorJsonObject = new ErrorJsonObject(102, "该用户尚未登录，无权查看收费课程");
                return JSONObject.toJSONString(errorJsonObject);
            }

            //查询该登录用户是否已经购买此课
            Order order = orderService.showAOrder(userId, classId);
            if (null == order) {
                ErrorJsonObject errorJsonObject = new ErrorJsonObject(103, "还未购买该课程，无权查看课程内容");
                return JSONObject.toJSONString(errorJsonObject);
            }
            lineList = lineService.showAClassAllLine(classId);
            UserClassKey userClassKey = new UserClassKey();
            userClassKey.setClassId(classId);
            userClassKey.setUserId(userId);
            //查询该用户是否以前学过该课程
            UserClass userClass = userClassService.showAUserClassInfo(userClassKey);
            if (null == userClass) {
                //插入一条记录
                UserClass userClass1 = new UserClass();
                userClass1.setUserId(userId);
                userClass1.setClassId(classId);
                //添加学习记录
                userClassService.addAUserClass(userClass1);
                //课程学习数量加一
                classService.reviseAClassCount(classId);
            }
            CorrectJsonObject correctJsonObject = new CorrectJsonObject("查询成功1");
            correctJsonObject.setData(lineList);
            return JSONObject.toJSONString(correctJsonObject);
        } else {

            try {
                // 获取当前访问对象
                UserSubject subject = (UserSubject) SubjectHolder.getSubject();

                //获取当前的登录信息
                UserEntity userEntity = (UserEntity) subject.getAuthorizationEntity();
                //获取当前的用户登录id
                int userId = userEntity.getUserId();
                //用户登录状态
                UserClassKey userClassKey = new UserClassKey();
                userClassKey.setClassId(classId);
                userClassKey.setUserId(userId);
                //查询该用户是否以前学过该课程
                UserClass userClass = userClassService.showAUserClassInfo(userClassKey);
                //第一次学习该课程
                if (null == userClass) {
                    //插入一条记录
                    UserClass userClass1 = new UserClass();
                    userClass1.setUserId(userId);
                    userClass1.setClassId(classId);
                    //添加学习记录
                    userClassService.addAUserClass(userClass1);
                    //课程学习数量加一
                    classService.reviseAClassCount(classId);
                }
                lineList = lineService.showAClassAllLine(classId);
                CorrectJsonObject correctJsonObject = new CorrectJsonObject("查询成功2");
                correctJsonObject.setData(lineList);
                return JSONObject.toJSONString(correctJsonObject);
            } catch (NullPointerException e) {
                //当前用户未登录
                //添加课程学习数量
                classService.reviseAClassCount(classId);
                lineList = lineService.showAClassAllLine(classId);
                CorrectJsonObject correctJsonObject = new CorrectJsonObject("查询成功3");
                correctJsonObject.setData(lineList);
                return JSONObject.toJSONString(correctJsonObject);
            }


        }
    }

    /**
     * 展示一条路线基本信息
     *
     * @param lineId 路线id
     * @return
     */
    @ResponseBody
    @RequestMapping(value = "/alineinfo", method = RequestMethod.GET, produces = "text/html;charset=UTF-8")
    String getALineInfo(@RequestParam(required = false) Integer lineId) {
        if (null == lineId) {
            ErrorJsonObject errorJsonObject = new ErrorJsonObject(105, "lineId参数为空");
            return JSONObject.toJSONString(errorJsonObject);
        }
        Line line = new Line();
        line = lineService.showALineInfo(lineId);
        CorrectJsonObject correctJsonObject = new CorrectJsonObject("查询成功");
        correctJsonObject.setData(line);
        return JSONObject.toJSONString(correctJsonObject);
    }

    /**
     * 展示一门课程所有路线级景点
     *此处需要将请求头token加上
     * @param classId
     * @return
     */
    @ResponseBody
    @RequestMapping(value = "/allinfo", method = RequestMethod.GET, produces = "text/html;charset=UTF-8")
    String getAClassAllLineInfo(@RequestParam(required = false) Integer classId) {
            if (null == classId) {
                ErrorJsonObject errorJsonObject = new ErrorJsonObject(110, "classId参数为空");
                return JSONObject.toJSONString(errorJsonObject);
            }
            //获得课程价格
            double price = classService.showAClassInfo(classId).getClassPrice();
            List<Line> lineList = new ArrayList<Line>();
            if (price != 0) {
                int userId;
                try {
                    // 获取当前访问对象
                    UserSubject subject = (UserSubject) SubjectHolder.getSubject();

                    //获取当前的登录信息
                    UserEntity userEntity = (UserEntity) subject.getAuthorizationEntity();
                    //获取当前的用户登录id
                    userId = userEntity.getUserId();
                } catch (NullPointerException e) {
                    ErrorJsonObject errorJsonObject = new ErrorJsonObject(111, "该用户尚未登录，无权查看收费课程");
                    return JSONObject.toJSONString(errorJsonObject);
                }

                //查询该登录用户是否已经购买此课
                Order order = orderService.showAOrder(userId, classId);
                if (null == order) {
                    ErrorJsonObject errorJsonObject = new ErrorJsonObject(112, "还未购买该课程，无权查看课程内容");
                    return JSONObject.toJSONString(errorJsonObject);
                }
                lineList = lineService.showAClassAllLineDetailAndLine(classId);
                UserClassKey userClassKey = new UserClassKey();
                userClassKey.setClassId(classId);
                userClassKey.setUserId(userId);
                //查询该用户是否以前学过该课程
                UserClass userClass = userClassService.showAUserClassInfo(userClassKey);
                if (null == userClass) {
                    //插入一条记录
                    UserClass userClass1 = new UserClass();
                    userClass1.setUserId(userId);
                    userClass1.setClassId(classId);
                    userClassService.addAUserClass(userClass1);
                    //课程学习数量加一
                    classService.reviseAClassCount(classId);
                }
                CorrectJsonObject correctJsonObject = new CorrectJsonObject("查询成功1");
                correctJsonObject.setData(lineList);
                return JSONObject.toJSONString(correctJsonObject);
            } else {

                try {
                    // 获取当前访问对象
                    UserSubject subject = (UserSubject) SubjectHolder.getSubject();

                    //获取当前的登录信息
                    UserEntity userEntity = (UserEntity) subject.getAuthorizationEntity();
                    //获取当前的用户登录id
                    int userId = userEntity.getUserId();
                    //用户登录状态
                    UserClassKey userClassKey = new UserClassKey();
                    userClassKey.setClassId(classId);
                    userClassKey.setUserId(userId);
                    //查询该用户是否以前学过该课程
                    UserClass userClass = userClassService.showAUserClassInfo(userClassKey);
                    //第一次学习该课程
                    if (null == userClass) {
                        //插入一条记录
                        UserClass userClass1 = new UserClass();
                        userClass1.setUserId(userId);
                        userClass1.setClassId(classId);
                        userClassService.addAUserClass(userClass1);
                        //课程学习数量加一
                        classService.reviseAClassCount(classId);
                    }
                    lineList = lineService.showAClassAllLineDetailAndLine(classId);
                    CorrectJsonObject correctJsonObject = new CorrectJsonObject("查询成功");
                    correctJsonObject.setData(lineList);
                    return JSONObject.toJSONString(correctJsonObject);
                } catch (NullPointerException e) {
                    //当前用户未登录
                    //添加课程学习数量
                    classService.reviseAClassCount(classId);
                    lineList = lineService.showAClassAllLineDetailAndLine(classId);
                    CorrectJsonObject correctJsonObject = new CorrectJsonObject("查询成功");
                    correctJsonObject.setData(lineList);
                    return JSONObject.toJSONString(correctJsonObject);
                }


            }
    }
}
